package keeto.com.vn.classified.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.database.BuiltinService;
import keeto.com.vn.classified.database.BuiltinServiceManager;
import keeto.com.vn.classified.network.IProtocol;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ChangePassActivity extends Activity implements OnClickListener {

	public static final int DIALOG_ID_PROGRESS = 1;
	private EditText edtOldPassword;
	private EditText edtNewPassword;
	private EditText edtConfirmNewPassword;
	private Button btnOK;
	private ViewHandler mHandler = new ViewHandler();
	private ChangePasswordListener mListener = new ChangePasswordListener();
	private String mNewPassword = "";
	private static InUIThreadHandler mReturnCodeHandler = null;
	private TextView tvTitle = null;
	private ImageButton btnBack = null;

	public static void actionLaunch(Context context) {
		Intent i = new Intent(context, ChangePassActivity.class);
		context.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_pass_layout);
		mReturnCodeHandler = new InUIThreadHandler();
		setTitle(R.string.item_change_password);
		ImageView imgSearch;
		imgSearch=(ImageView)findViewById(R.id.btn_sub_title_search_home);
		imgSearch.setVisibility(View.GONE);
		tvTitle = (TextView) findViewById(R.id.tv_sub_title_text);
		btnBack = (ImageButton) findViewById(R.id.btn_sub_title_back);

		edtOldPassword = (EditText) findViewById(R.id.old_password);
		edtNewPassword = (EditText) findViewById(R.id.new_password);
		edtConfirmNewPassword = (EditText) findViewById(R.id.confirm_password);
		btnOK = (Button) findViewById(R.id.btn_update);

		tvTitle.setText(R.string.item_change_password);

		btnOK.setOnClickListener(this);
		btnBack.setOnClickListener(this);
	}

	class ViewHandler {
	}

	class ChangePasswordListener extends ClassifiedListener{

		@Override
		public void finishChangePass(final int returnCode, String resultMsg) {
			// TODO Auto-generated method stub
			super.finishChangePass(returnCode, resultMsg);
			runOnUiThread(new Runnable() {
				public void run() {
					if (returnCode==IProtocol.STATUS_CODE_OK_200) {
						Toast.makeText(ChangePassActivity.this,(ChangePassActivity.this).getResources().getString(R.string.text_successful), Toast.LENGTH_SHORT).show();
						finish();
					}else if(returnCode==IProtocol.STATUS_CODE_ACCOUNT_ALREADY_EXIST_672){
						Toast.makeText(ChangePassActivity.this,(ChangePassActivity.this).getResources().getString(R.string.text_change_pass_failed), Toast.LENGTH_SHORT).show();
						
					}
				}
			});
		}
		
	}

	private void validatePasswordAndSubmit() {

		BuiltinService bs = BuiltinServiceManager.getInstance(this)
				.getBuitinService();

		mNewPassword = edtNewPassword.getText().toString();

		if (bs == null
				|| !bs.getPassword()
						.equals(edtOldPassword.getText().toString())) {
			setErrorMsg(getString(R.string.change_password_err_do_not_correct),
					edtOldPassword);
			edtOldPassword.requestFocus();
			return;
		}

		if (edtNewPassword.getText().toString().equals("")
				|| edtNewPassword.getText().toString().length() < 6
				|| edtNewPassword.getText().toString().length() > 16) {

			setErrorMsg(getString(R.string.register_password_requirement),
					edtNewPassword);
			edtNewPassword.requestFocus();
			return;
		}

		// password validation
		Pattern p2 = Pattern.compile("^[a-z0-9A-Z]{6,20}$");
		Matcher m2 = p2.matcher(edtNewPassword.getText().toString());
		boolean isMatch2 = m2.matches();
		if (!isMatch2) {
			setErrorMsg(getString(R.string.register_invalid_password),
					edtNewPassword);
			edtNewPassword.requestFocus();
			return;
		}

		if (!edtNewPassword.getText().toString()
				.equals(edtConfirmNewPassword.getText().toString())) {
			setErrorMsg(
					getString(R.string.register_confirm_password_requirement),
					edtConfirmNewPassword);
			edtConfirmNewPassword.requestFocus();
			return;
		}
		showDialog(DIALOG_ID_PROGRESS);
		// call the change password method.
		 ClassifiedCore.getInstance(getApplication()).taskChangePass(mListener,edtOldPassword.getText().toString(), edtNewPassword.getText().toString(),MainApplication.getAuthenKey());

	}

	private void setErrorMsg(String msg, EditText viewId) {
		int ecolor = Color.RED; // whatever color you want
		String estring = msg;
		ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
		SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
		ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
		viewId.setError(ssbuilder);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_update:
			validatePasswordAndSubmit();
			break;

		case R.id.btn_sub_title_back:
			finish();
			break;
		}
	}
}
